// src/store/tag-list.js
import { defineStore } from "pinia";

export const useTagStore = defineStore('tag', {
  state: () => ({
    tagList: [
      // 初始化默认展示【首页】标签导航
      {
        path: '/home/totalscore',
        name: 'home',
        meta: {title: '首页'}
      }
    ]
  }),
  getters: {
    tagListGetter: state => state.tagList
  },
  actions: {
    addTag(item) {
      // 检查是否已存在相同路径的tag
      console.log(item,"执行了addTag")
      const exists = this.tagList.some(tag => tag.path === item.path)
      if (!exists) {
        this.tagList.push(item)
      }
    },
    delTag(item) {
      // 找出要删除的tag的索引值
      const i = this.tagList.findIndex(v => v.path === item.path)
      this.tagList.splice(i, 1)
    },
    clearTempData(){
      this.tagList = [
        {
          path: '/home/totalscore',
          name: 'home',
          meta: {title: '首页'}
        }
      ]
    }
  },
  // 持久化
  persist: {
    enabled: true, // 开启
    strategies: [
      {
        key: 'tagList',
        storage: sessionStorage
      }
    ]

  }
})
